package com.rosh.rpc.core.netty.codec;

import com.rosh.rpc.core.data.RpcResponse;
import com.rosh.rpc.core.utils.ProtostuffUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;

import java.util.List;

/**
 * @Description:
 * @Author: rosh
 * @Date: 2022/6/14 22:35
 */
public class RpcResponseDecoder extends MessageToMessageDecoder<ByteBuf> {
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        int length = byteBuf.readableBytes();
        byte[] bytes = new byte[length];
        byteBuf.readBytes(bytes);
        //反序列化
        RpcResponse rpcResponse = ProtostuffUtil.deserialize(bytes, RpcResponse.class);
        list.add(rpcResponse);
    }
}
